package com.starry.feign;

import com.starry.core.common.tenant.context.TenantContextHolder;
import com.starry.core.common.token.context.TokenContextHolder;
import com.starry.core.redis.service.RedisService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import static com.starry.core.common.constants.CommonConstant.*;

/**
 * feign 传递Request header
 */
@Slf4j
@Component
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        Boolean oldIgnore = RedisService.IGNORE_CACHE.get();
        RedisService.IGNORE_CACHE.set(true);
        requestTemplate.header(INTERNAL_HEADER, INTERNAL_HEADER_VALUE);
        // 设置租户id
        Long tenantId = TenantContextHolder.getTenantId();
        if (tenantId != null) {
            requestTemplate.header(HEADER_TENANTID, TenantContextHolder.getTenantId().toString());
        }
        requestTemplate.header(Authorization_HEADER, TokenContextHolder.getToken());
        RedisService.IGNORE_CACHE.set(oldIgnore);
    }

}
